package com.fei.yygh.hosp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fei.yygh.excption.YyghException;
import com.fei.yygh.model.hosp.HospitalSet;
import com.fei.yygh.hosp.service.HospitalSetService;
import com.fei.yygh.result.Result;
import com.fei.yygh.util.MD5;
import com.fei.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院管理设置")
@CrossOrigin  //不添加该注释，前端项目无法请求到后端接口解决跨域的问题
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 查询医院设置表的所有信息
     * LongFei
     */
    @ApiOperation(value = "查询所有医院设置信息")
    @GetMapping("/findAll")
    public Result findAll() {
//       调用service方法进行查询
        List<HospitalSet> hospitalSetList = hospitalSetService.list();
        return Result.ok(hospitalSetList);
    }

    /**
     * 删除医院设置表信息
     * LongFei
     */
    @ApiOperation(value = "逻辑删除")
    @DeleteMapping("{id}")
    public Result delhosp(@PathVariable Long id ) {
        boolean b = hospitalSetService.removeById(id);
        if (b){
            return Result.ok(b);
        }else {
            return Result.fail(b);
        }
    }

    /**
     * longfei
     * 条件查询带分页
     */
    @ApiOperation("分页查询")
    @PostMapping ("/findPageHospSet/{current}/{limit}")  // current当前页,limit每页记录数
    public Result findPageHospSet(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //Page对象
        Page<HospitalSet> page = new Page<>(current, limit);
        //构造器，构造查询条件
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        //非空判断
        if (!StringUtils.isEmpty(hosname)){
            queryWrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)){
            queryWrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }
        //进行分页查询
        Page<HospitalSet> pageResult = hospitalSetService.page(page, queryWrapper);
        return Result.ok(pageResult);
    }


    /**
     * longfei
     * 添加医院设置
     */
    @ApiOperation("添加医院设置")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {

        //设置状态
        hospitalSet.setStatus(1);

        //设置秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(100)));

        //调用save方法进行添加数据
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok(save);
        }else {
            return Result.fail(save);
        }

    }

    /**
     * longfei
     * 根据ID查询医院设置信息
     */
    @ApiOperation("根据ID进行查询")
    @GetMapping("findByid/{id}")
    public Result findById(@PathVariable Long id) {
        HospitalSet byId = hospitalSetService.getById(id);
        if (byId != null) {
            return Result.ok(byId);
        }else {
            return Result.fail();
        }
    }

    /**
     * longfei
     * 修改医院设置
     */
    @ApiOperation("更新医院设置")
    @PostMapping("/update")
    public Result update(@RequestBody HospitalSet hospitalSet) {
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
            return Result.ok(b);
        }else {
            return Result.fail(b);
        }
    }

    /**
     * longfei
     * 医院设置批量删除
     */
    @ApiOperation("批量删除")
    @DeleteMapping("/deleteHospital")
    public Result deleteHospital(@RequestBody List<Long> idList) {
        boolean byIds = hospitalSetService.removeByIds(idList);
        if (byIds){
            return Result.ok(byIds);
        }else {
            return Result.fail(byIds);
        }
    }

    /**
     * longfei
     * 医院设置的锁定和解锁 ，数据中的status
     */
    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("/lockHospital/{id}/{status}")
    public Result lockHospital(@PathVariable Long id, @PathVariable Integer status) {
        //根据ID查询数据
           HospitalSet byId = hospitalSetService.getById(id);
           byId.setStatus(status);
           boolean b = hospitalSetService.updateById(byId);
           if (b){
               return Result.ok(b);
           }else {
               return Result.fail(b);
           }
    }

    /**
     * lognfei
     * 短信发送，传递Key值
     */
    @PutMapping("/sedKey/{id}")
    public Result sedKey(@PathVariable Long id) {
        //ID查询
        HospitalSet byId = hospitalSetService.getById(id);
        String hoscode = byId.getHoscode();
        String signKey = byId.getSignKey();
        //发送短信，TODO
        return Result.ok(byId);
    }




}
